<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: ajax.user.php Sat Jul 18 12:44:55 CST 2009
 */

include "conf.inc";
header("Content-Type:text/html; charset="._CHARSET);

include XOOPS_ROOT_PATH."/class/template.php";
$xoopsTpl = new XoopsTpl();

$group_id = isset($_GET["gid"]) ? intval($_GET["gid"]) : 0;
$op = isset($_GET["op"]) ? trim($_GET["op"]) : "";

$group_handler = xoops_getmodulehandler("group");
$memb_handler = xoops_getmodulehandler("memb");
$event_handler = xoops_getmodulehandler("event");

/** 检查来访者权限 **/
if ( empty($xoopsUser) || !is_object($xoopsUser) ) {
    $op = "noperm";
} 

switch ($op) {
    case "mygroups":
        $uid = $xoopsUser->getVar("uid");
        $_my_ids = $members_handler->getMyGroupsIds($uid);
        $_my_groups = $groups_handler->getMyGroupsList($_my_ids);
        $html = "";
        if ( $_my_groups ) {
            $html .= "<div class=\"tablebox\"><table width=\"100%\" cellspacing=\"0\">";
            foreach ( $_my_groups as $k=>$v ) {
                $html .= "<tr><td><a href=\"groups.php?gid={$k}\">{$v["group_name"]}</a></td></tr>";
            }
            $html .= "</table></div>";
        } else {
            $html .= "<div class=\"tablebox\">"._GP_MYGROUPS_NOJOIN."</div>";
        }
        echo $html;
        exit();
    break;
    
    case "outgroup":
        $group_obj = $groups_handler->get($group_id);
        if ( true === $group_obj->isCreateUser($xoopsUser->getVar("uid")) ) {
            echo _GP_GROUP_LOGOUT_CREATEUSERDESC;
            exit();
        }
        if ( $members_handler->outGroup($xoopsUser->getVar("uid"),$group_id) ) {
            $groups_handler->changeMembers($group_id,-1);
            /** 产生一个事件 **/
		    $events_handler->eventApi($xoopsUser->getVar("uid"),$group_id,103,$group_obj->getVar("group_name","n"),$group_id,"");
            echo sprintf(_GP_GROUP_LOGOUT_DESC,$group_obj->getVar("group_name","n"));
        } else {
            echo _NOPERM;
        }
        exit();
    break;
    
    case "joingroup":
        $group_obj = $groups_handler->get($group_id);
        $status = ( true === $group_obj->joinStatus() ) ? 1 : 0;
        $decs = !empty($status) ? _GP_GROUP_JOIN_DESC_1 : _GP_GROUP_JOIN_DESC_2 ;
        if ( $members_handler->addGroupMember($group_id,$xoopsUser->getVar("uid"),0,$status) ) {
            $groups_handler->changeMembers($group_id,1);
            /** 产生一个事件 **/
            $_event_type = !empty($status) ? 101 : 102;
		    $events_handler->eventApi($xoopsUser->getVar("uid"),$group_id,$_event_type,$group_obj->getVar("group_name","n"),$group_id,"");
            echo sprintf($decs,$group_obj->getVar("group_name","n"));
        } else {
            echo _NOPERM;
        }
        exit();
    break;
    
    case "creategroup":
        xoops_loadLanguage("user","groups");
        if ( !$xoopsModuleConfig["isusercreategroup"] ) {
            echo _US_ISUSERCREATEGROUP;
            exit();
        }
        $ac = isset($_POST["ac"]) ? trim($_POST["ac"]) : "";
        switch ($ac) {
            case "savegroup":
                $group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0;
                $cat_id = isset($_POST["cat_id"]) ? intval($_POST["cat_id"]) : 0;
        	    $group_name = isset($_POST["group_name"]) ? trim($_POST["group_name"]) : "";
        	    $group_description = isset($_POST["group_description"]) ? trim($_POST["group_description"]) : "";
        	    $uid = $xoopsUser->getVar("uid");
        	    
        	    $category_handler = xoops_getmodulehandler("category","groups");
        	    $status = $category_handler->getCateToGroupStatus($cat_id);
        	    
        		if (!$GLOBALS["xoopsSecurity"]->check()) {
                    echo _GP_ERROR_OPERATION;
                    exit();
                }
                
        		if ( $group_id = $groups_handler->setGroup($group_id,$cat_id,$uid,$group_name,$group_description,$status,"") ) {
        		    /** 默认权限 **/
        		    $groups_handler->setGroupPerm($group_id,
        		    $group_create_default_perm["group_access_permission"],
        		    $group_create_default_perm["group_post_permission"],
        		    $group_create_default_perm["group_reply_permission"],
        		    $group_create_default_perm["group_join_permission"]);
        		    /** 产生一个事件 **/
        		    $events_handler->eventApi($uid,$group_id,100,$group_name,$group_id,$group_description);
            		/** 获取群对像 **/
            		$group_obj = $groups_handler->get($group_id);
        		    /** 该群组增加一个成员 **/
        		    $members_handler->addGroupmember($group_id,$uid,1);
            		/** 修改群组成员数 **/
            		$groups_handler->changeMembers($group_id,1);
        		    /** 分类数量要修改 **/
        		    $category_handler->changeGroups($cat_id,1);
        		    /** 设置默认图片 **/
    		        $logo = "group.gif";
    		        $groups_handler->uploadGroupLogo($group_id,$logo);
    		        
        			$stop = sprintf(_GP_CREATE_SUCCEED,$group_name,$group_id);
        		} else {
        			$stop = _US_FAILED;
        		}
        		echo $stop;
        		exit();
            break;
            
            default:
                $formUrl = _GROUPS_URL."/ajax.user.php?op=creategroup";
                $group_obj = $group_handler->get();
        		$groups_arr = $group_obj->getValues();
        		include(dirname(__FILE__)."/user/form.us.groups.create.php");
        		$xoopsTpl->display("db:groups_group_create.html");
            break;
        }
    break;
    
    case "noperm":
    default:
        echo _GP_GROUP_NEED_LOGIN;
        exit();
    break;
}
?>